﻿using Janus.BaseEntitys;
using Janus.Cores.Extension;
using Janus.Cores.Linq;
using Janus.Mes.DTO;
using Janus.Mes.Services.Interface;
using Janus.Mes.VO;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace Janus.Mes.WebClient.Areas.Quality.Controllers.SampleProcess
{
    public class SampleProcessController : BaseController
    {
        /// <summary>
        /// 系统参数设置首页（返回数据列表）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View(new List<SampProcessVO>());
        }

        /// <summary>
        /// 新增、修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Create(string id)
        {
            SampProcessVO sampprocess = null;
            ViewBag.IsCreate = true;
            if (!string.IsNullOrEmpty(id))
            {
                ViewBag.IsCreate = false;
                Querier query = new Querier(" SampPCode={@0} ", new List<object>() { id });
                var model = SampProcessServices.FindOne(query);
                sampprocess = model.ToEntity<SampProcessVO>();
            }
            else
            {
                sampprocess = base.InitModel<SampProcessVO>(sampprocess);
            }
            return View(sampprocess);
        }

        #region 新增/编辑提交
        /// <summary>
        /// 新增/编辑提交
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public string Create(FormCollection form)
        {
            try
            {
                var result = false;
                SampProcessVO model = new SampProcessVO();
                model = base.SetModel<SampProcessVO>(form);
                var sampprocess = model.ToEntity<SampProcessDTO>();
                result = SampProcessServices.Save(sampprocess) > 0;
                if (result)
                {
                    ajaxStr = base.AjaxResultSucess;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        #region 主键是否存在
        [HttpPost]
        public string IsExist()
        {
            string data = Request["data"];
            Querier query = new Querier(" SampPCode={@0} ", new List<object>() { data });
            bool result = SampProcessServices.IsExists(query);
            if (!result)
            {
                ajaxStr = base.AjaxResultSucess;
            }
            else
            {
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }
        #endregion

        public ActionResult Select()
        {
            return View();
        }
        /// <summary>
        /// 检验项目
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectItem()
        {
            return View();
        }
        /// <summary>
        /// 检验地点
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectPlace()
        {
            return View();
        }
        /// <summary>
        /// 检验特性
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectFeatures()
        {
            return View();
        }
        /// <summary>
        /// 检验环境
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectEMT()
        {
            return View();
        }
        /// <summary>
        /// 检验设备
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectDevice()
        {
            return View();
        }
    }
}